var vm_customyue = new Vue({
    el: "#customtuikuan",
    data: {
        deptid: $("#deptid").val(),		// 单位ID

        tuikuanlist: [],// 退款list
        piliangshoukuanlist:'', //批量收款list

        piliangshoukuanids: [],//退款ids

        shoukuanzhuangtai:0, //收款状态
        deptName:'',  //单位名称

        pageindex: 1,		// 查询分页初始化页
        pagesize: 10,		// 查询分页初始化页面大小
        pageCount: 0,		// 总页数
        recordCount: 0,	// 总记录数
        targetpage: '', 	// 目标页

    },
    created: function () {
        var _this = this;
        _this.bindTuikuanList();

    },
    watch: {
        //监控分页情况，刷新列表
        pageindex: function () {
            this.bindTuikuanList();
        },

        pagesize: function () {
            //
            this.bindTuikuanList();
        },
        shoukuanzhuangtai : function(){
            this.bindTuikuanList();
        }
    },
    methods: {

        //查询下级单位退款计划列表
        bindTuikuanList: function () {
            var _this = this;
            $.get('/system/tuikuan/findtuikuanlist', {
                deptId: _this.deptid,
                pageIndex: _this.pageindex,
                pageSize: _this.pagesize,
                status : _this.shoukuanzhuangtai,
                deptName:_this.deptName
            }, function (ppData) {
                if (ppData != null) {
                    if (ppData.result == "1") {
                        var data = ppData.resultContent;
                        _this.tuikuanlist = data.tuikuanList;
                        _this.tuikuanlist.forEach((item) => {
                            if(item.shoudaoshijian)item.shoudaoshijian = _this.transformTimestamp(item.shoudaoshijian);
                            if(item.tuikuanshijian)item.tuikuanshijian = _this.transformTimestamp(item.tuikuanshijian);
                        })
                        var pageInfo = data.pageInfo;
                        _this.pageindex = pageInfo.pageIndex;
                        _this.pageCount = pageInfo.pageCount;
                        _this.recordCount = pageInfo.recordCount;
                        _this.targetpage = '';
                    } else {
                        layer.alert(ppData.message)
                    }
                }
            }, "json");
        },

        toShoukuan: function () {
            var _this = this;

            var chk_value =[];

            _this.piliangshoukuanlist = [];
            _this.piliangshoukuanids = '';

            $('input[name="checkbox"]:checked').each(function(){
                chk_value.push($(this).val());
            });
            if (chk_value.length==0){
                layer.alert('您还没有选择任何退款！');
            }else{
                for(var i=0;i<chk_value.length;i++){
                    for(var j=0;j<_this.tuikuanlist.length;j++){
                        if (chk_value[i]==_this.tuikuanlist[j].id){
                            _this.piliangshoukuanlist.push(_this.tuikuanlist[j]);
                            _this.piliangshoukuanids = _this.piliangshoukuanids+_this.tuikuanlist[j].id+",";
                        }
                    }
                }
                $("#piliangModalLabel").html("批量收款");
                $("#modal-dialog").attr("style","width:1200px;");
                $("#piliangshoukuanModal").modal();
            }
        },
        Shoukuan:function (id) {
            var _this = this;
            $.modal.confirm("是否确认收款",function () {
                    $.post('/system/tuikuan/querentuikuan', {
                        ids: id,
                        deptId:_this.deptid
                    }, function (ppData) {
                        if (ppData != null) {
                            if (ppData.result == 1) {
                                layer.open({
                                    time: 1000,
                                    btn: [],
                                    content: "确认退款成功!",
                                });
                                _this.refresh();
                            } else {
                                layer.alert(ppData.message);
                            }
                        }
                    }, "json");
            }
            )
        },
        piliangshoukuan:function () {
            var _this = this;
            $.post('/system/tuikuan/querentuikuan', {
                ids: _this.piliangshoukuanids,
                deptId:_this.deptid
            }, function (ppData) {
                if (ppData != null) {
                    if (ppData.result == 1) {
                        layer.open({
                            time: 1000,
                            btn: [],
                            content: "确认退款成功!",
                        });
                        _this.refresh();
                        $('input[name="checkbox"]:checked').each(function(){
                            this.checked = false
                        });
                        $("#piliangshoukuanModal").modal('hide');
                    } else {
                        layer.alert(ppData.message);
                    }
                }
            }, "json");
        },
        refresh:function(){
            var _this = this;
            _this.bindTuikuanList();
        },

        transformTimestamp:function(timestamp) {
            let a = new Date(timestamp).getTime();
            const date = new Date(a);
            const Y = date.getFullYear() + '-';
            const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
            const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + '  ';
            const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
            const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
         // const s = date.getSeconds(); // 秒
            const dateString = Y + M + D + h + m;
         // console.log('dateString', dateString); // > dateString 2021-07-06 14:23
          return dateString;
        },

        reset:function(){
            var _this = this;
            _this.shoukuanzhuangtai = 0;
            _this.deptName = '';
            _this.bindTuikuanList()
        },

        //全选按钮的点击事件
        checkAll(){
            //获取id的dom元素
            let Id = document.getElementById('allAndNotAll');
            //获取类名为chBox的dom元素
            let chBox = document.getElementsByName('checkbox');
            let len = chBox.length;
            //输出观察

            for(let i=0;i<len;i++){
                //当全选按钮为true时，全部按钮都为true，否则相反
                if(Id.checked){
                    chBox[i].checked=true;
                }
                else {
                    chBox[i].checked=false;
                }
            }
        },
        //单个按钮的点击事件
        checkOne(){
            let count=0;
            let chBox = document.getElementsByName('checkbox');
            let len = chBox.length;
            for(let i=0;i<len;i++){
                //记录单选按钮为true的数量
                if(chBox[i].checked){
                    count++;
                }
            }
            //当单选按钮都为true时，全选按钮也为true，否则为false
            if(count==len){
                document.getElementById('allAndNotAll').checked = true;
            }else {
                document.getElementById('allAndNotAll').checked = false;
            }
        },

        //首页
        firstPage: function () {
            this.pageindex = 1;
        },
        //尾页
        lastPage: function () {
            this.pageindex = this.pageCount;
        },
        //上一页
        prePage: function () {
            var prePage = ((this.pageindex - 1) <= 0) ? 1 : (this.pageindex - 1);
            this.pageindex = prePage;
        },
        //下一页
        nextPage: function () {
            var nextPage = ((this.pageindex + 1) >= this.pageCount) ? this.pageCount : (this.pageindex + 1);
            this.pageindex = nextPage;
        },
        //跳到目标页
        targetPage: function () {
            if (this.targetpage != '' && this.targetpage <= this.pageCount && this.targetpage > 0) {
                this.pageindex = this.targetpage;
                this.targetpage = '';
            }
        },

    },
})